<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form :inline="true" :model="searchParams">
        <el-form-item label="楼栋名称" prop="buildingName">
          <el-input v-model="searchParams.buildingName" placeholder="请选择楼栋名称" readonly="readonly">
            <i slot="suffix" class="el-input__icon el-icon-search" @click="findBuilding"></i>
          </el-input>
        </el-form-item>
        <el-form-item label="房号">
            <el-input v-model="searchParams.roomNumber" placeholder="请输入房号" maxlength="32"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table
        :data="pageData.list"
        border
        v-loading="pageData.loading"
        size="mini"
        header-cell-class-name="table-header-cell-font"
        cell-class-name="table-cell-font"
        @row-dblclick="roomSelected">
              <el-table-column prop="buildingName" label="楼栋名称"></el-table-column>
        <el-table-column prop="roomNumber" label="房号"></el-table-column>
        <el-table-column prop="floorNumber" label="所在楼层"></el-table-column>
        <el-table-column prop="grossFloorArea" label="建筑面积"></el-table-column>
        <el-table-column prop="netFloorArea" label="实用面积"></el-table-column>
      </el-table>
      <div class="table-pagination">
        <el-pagination
          @current-change="paginationChange"
          :current-page="pageData.current"
          :page-sizes="pageData.pageSizes"
          :page-size="pageData.size"
          background
          layout="total, prev, pager, next, jumper"
          :total="pageData.count">
        </el-pagination>
      </div>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="buildingSelectorVisible" v-if="buildingSelectorVisible" title="楼栋信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <building-selector @building-selected-event="handleBuildingSelectedEvent">
      </building-selector>
    </el-dialog>
  </div>
</template>

<script>
import roomApi from '@/api/roomApi'
import BuildingSelector from "@/views/building/BuildingSelector.vue";
import listQueryMixin from '@/mixins/listQueryMixin'
export default {
  name: 'RoomSelector',
  components: {BuildingSelector,},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        buildingId: '',
        buildingName: '',
        roomNumber: ''
      },
      buildingSelectorVisible: false,
    }
  },
  methods: {
    getPageData (p) {
      return roomApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        buildingId: '',
        buildingName: '',
        roomNumber: ''
      }
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    roomSelected (row) {
      this.$emit('room-selected-event', {
        roomId: row.id,
        roomNumber: row.roomNumber,
      })
    },
    findBuilding () {
      this.buildingSelectorVisible = true
    },
    handleBuildingSelectedEvent (selectedBuilding) {
      this.buildingSelectorVisible = false
      if (selectedBuilding && 'buildingId' in selectedBuilding && this.searchParams.buildingId !== selectedBuilding['buildingId']) {
        this.searchParams.buildingId = selectedBuilding['buildingId']
        this.searchParams.buildingName = selectedBuilding['buildingName']
      }
    },
    onBack () {
      this.$emit('room-selected-event')
    }
  }
}
</script>

<style lang="scss">
</style>
